<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
		<title></title>
		<style type="text/css">
			html {
				height: 100%
			}
			
			body {
				height: 100%;
				margin: 0px;
				padding: 0px
			}
			
			#container {
				height: 100%
			}
		</style>
		<script type="text/javascript" src="https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=10W14fXXO0VElPqlcgbxfGxqTBfExGlo"></script>
		<script type="text/javascript">
			window.onload = function() {
				//判断浏览器是否支持geolocation
				if(navigator.geolocation) {
					navigator.geolocation.getCurrentPosition(success, errorFn, options);
				}
				//成功
				function success(position) {
					//coords.latitude	十进制数的纬度
					//coords.longitude	十进制数的经度
					var lat = position.coords.latitude;
					var lng = position.coords.longitude;
					alert('纬度:'+lat+',经度:'+lng); 
					
					// 百度地图API功能
					//GPS坐标
					var x = lng;
					var y = lat;
					var ggPoint = new BMapGL.Point(x, y);

					//地图初始化
					var bm = new BMapGL.Map("container");
					bm.centerAndZoom(ggPoint, 15);
					bm.addControl(new BMapGL.ZoomControl());

					//添加gps marker和label
					var markergg = new BMapGL.Marker(ggPoint);
					bm.addOverlay(markergg); //添加GPS marker
					var labelgg = new BMapGL.Label("未转换的GPS坐标（错误）", {
						offset: new BMapGL.Size(10, -10)
					});
					markergg.setLabel(labelgg); //添加GPS label

					//坐标转换完之后的回调函数
					translateCallback = function(data) {
						if(data.status === 0) {
							var marker = new BMapGL.Marker(data.points[0]);
							bm.addOverlay(marker);
							var label = new BMapGL.Label("转换后的百度坐标（正确）", {
								offset: new BMapGL.Size(10, -10)
							});
							marker.setLabel(label); //添加百度label
							bm.setCenter(data.points[0]);
						}
					}
					setTimeout(function() {
						var convertor = new BMapGL.Convertor();
						var pointArr = [];
						pointArr.push(ggPoint);
						convertor.translate(pointArr, COORDINATES_WGS84, COORDINATES_BD09, translateCallback)
					}, 1000);
				}
				//失败
				function errorFn(error) {
					switch(error.code) {
						case error.PERMISSION_DENIED:
							alert("定位失败,用户拒绝请求地理定位");
							break;
						case error.POSITION_UNAVAILABLE:
							alert("定位失败,位置信息是不可用");
							break;
						case error.TIMEOUT:
							alert("定位失败,请求获取用户位置超时");
							break;
						case error.UNKNOWN_ERROR:
							alert("定位失败,定位系统失效");
							break;
					}
				}
				//参数
				var options = {
					//是否允许使用高精度
					enableHighAccuracy: false,
					//超时时间
					timeout: 5000,
					//缓存的过期时间
					maximumAge: 60000
				}
			}
		</script>
	</head>

	<body>
		<div id="container"></div>
	</body>

</html>